package com.abigdreamer.spider.simple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author Darkness
 * @date 2013-6-2 下午02:35:38
 * @website http://www.abigdreamer.com
 * @version V1.0
 */
public class Catcher {

	private static ArrayList<String> threads = new ArrayList<String>();// 存储未处理URL
	public static boolean isFinished = false;// 是否已经把所有的链接存到threads了
	private int finishCount = 0;
	private int totalCount = 1000;

	public static void main(String[] args) {
		//new Catcher().process();
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "darkness");
		map.put("sex", "男");
		
	}
	
	public synchronized String getUrl() {
		if (threads.size() > 0) {
			String tmp = threads.get(0);
			threads.remove(0);
			finishCount++;
			System.out.println("完成度："+((finishCount*100.0)/totalCount)+"%");
			return tmp;
		} else
			return null;
	}

	public void process() {
		// 处理预处理
		// 下面开10个线程等待处理
		for (int i = 0; i < 20; i++) {
			new Thread(new Processer(this)).start();
		}

		// 进入翻页的处理
		for (int j = 1; j < 1000; j++) {// 从第一页翻到最后一页
			threads.add("http://hkbici.com/forum-2-" + j + ".html");// 把URL存进去
		}
		isFinished = true;
	}

}

class Processer implements Runnable {
	Catcher c;

	public Processer(Catcher c) {
		this.c = c;
	}

	public void run() {
		String tmp = null;
		while ((tmp = c.getUrl()) != null || !c.isFinished) {// 当还有记录时就处理
			if (tmp != null) {
				// 处理将一条信息保存到数据库
			} else {// 如果没标志处理则休眠一秒再重新开始处理
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}
}